package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import connection.DbConnection;

import bean.Question;

import model.CreateSQuestionImpl;
public class AddSQuestionServlet extends HttpServlet {



	/**
	 * 
	 */
	private static final long serialVersionUID = 5633528104704791650L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost (request, response);
	}
	
	public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("GB18030");
		String text = request.getParameter("text");
		String status = "0";
		String template = request.getParameter("TemplateQuestion");
		String cor1 = request.getParameter("Correct1");
		String cor2 = request.getParameter("Correct2");
		String cor3 = request.getParameter("Correct3");
		String cor4 = request.getParameter("Correct4");
		String cor5 = request.getParameter("Correct5");
		String numberonly = request.getParameter("numberonly");
		String lowest = request.getParameter("lowest");
		String highest = request.getParameter("highest");
		String font = request.getParameter("font");
		String size = request.getParameter("size");
		String color = request.getParameter("color");
		String timer= request.getParameter("timer");
		String RC= request.getParameter("RC");
		String CAI= request.getParameter("CAI");
		String pollid = (String)request.getSession().getAttribute("pid");
		String pollver = request.getParameter("pver");
		String pollname = request.getParameter("pname");
		String polldes = request.getParameter("pdes");
		if(lowest == null || highest == null){
			lowest = highest = "0";
		}
		if(cor1 != null){
			if(cor2 != null){
				if(cor3 != null){
					if(cor4 != null){
						if(cor5 == null){
							cor5 = "null";
						}
					}else{
						cor4 = "null";
					}
				}else{
					cor3 = "null";
				}
			}else{
				cor2 = "null";
			}
		}else{
			cor1 = "null";
		}
		/*Question question = new Question();
		question.setText(text);
		question.setStatus(Integer.parseInt(status));System.out.println(status);
		//question.setTemplateQuestion(Integer.parseInt(template));
		if(cor1 != null){
			question.setCorrect1(cor1);
		}else{
			question.setCorrect1("");
		}
		question.setCorrect2(cor2);
		question.setCorrect3(cor3);
		question.setCorrect4(cor4);
		question.setCorrect5(cor5);
		System.out.println(numberonly);
		question.setNumberOnly(Integer.parseInt(numberonly));System.out.println(numberonly);
		question.setLowest(Double.parseDouble(lowest));System.out.println("1");
		question.setHighest(Double.parseDouble(highest));System.out.println("1");
		question.setFont(font);System.out.println("1");
		question.setFontSize(Integer.parseInt(size));System.out.println(size);
		question.setColor(color);System.out.println("1");
		question.setTimer(timer);System.out.println("1");
		question.setResCounter(RC);System.out.println("1");
		question.setCAI(CAI);
		*/
		try{
			DbConnection db = new DbConnection();
			Connection conn = db.getConn();
			
			System.out.println("*****//RESULTSET NEXT");
			String sql = "insert into Question (text, status, TemplateQuestion, correct1, correct2, correct3, correct4, correct5, numberonly, lowest, highest, font, fontsize, color, timer, rescounter, cai) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			PreparedStatement st = conn.prepareStatement(sql);

			st.setString(1, "'"+text+"'");
			st.setString(2, "0");
			st.setString(3, template);
			st.setString(4, cor1);
			st.setString(5, cor2);
			st.setString(6, cor3);
			st.setString(7, cor4);
			st.setString(8, cor5);
			st.setString(9, numberonly);
			st.setString(10, lowest);
			st.setString(11, highest);
			st.setString(12, font);
			st.setString(13, size);
			st.setString(14, color);
			st.setString(15, timer);
			st.setString(16, RC);
			st.setString(17, CAI);
			
			//also insert into adjacent table poll_question
			st.executeQuery();
			db.close();
			
			String id = request.getSession().getAttribute("pollid").toString();
			String username = request.getSession().getAttribute("username").toString();
			DbConnection db3 = new DbConnection();
			Connection conn3 = db.getConn();
			sql = "INSERT INTO User_Question VALUES('"+username+"', (SELECT MAX(Question.questionid) FROM Question))";
			PreparedStatement st3 = conn3.prepareStatement(sql);
			st3.executeQuery(sql);
			db3.close();
			
			DbConnection db2 = new DbConnection();
			Connection conn2 = db.getConn();
			sql = "INSERT INTO Poll_Question(PollID, PollVersion, QuestionID, PollName) VALUES("+id+", (SELECT MAX(version) FROM Poll WHERE ID="+id+"), "+
				"(SELECT MAX(Question.questionid) FROM Question), '" 
				+request.getSession().getAttribute("pollName")+"')";
			PreparedStatement st2 = conn2.prepareStatement(sql);
			st2.executeQuery(sql);
			db2.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		String forward= "Addquestion.jsp?id='"+pollid+"'&ver='"+pollver+"'&name='"+pollname+"'&des='"+polldes+"'";
		RequestDispatcher rd=request.getRequestDispatcher(forward);
		rd.forward(request, response);
	}
}
